package cn.highedu.coolshark.product.controller;

import cn.highedu.coolshark.common.JsonResult;
import cn.highedu.coolshark.product.pojo.dto.AlbumAddNewDTO;
import cn.highedu.coolshark.product.pojo.dto.PictureAddNewDTO;
import cn.highedu.coolshark.product.pojo.vo.PictureListItemVO;
import cn.highedu.coolshark.product.service.IAlbumService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理相册相关请求的控制器类
 */
@RestController
@Slf4j
@RequestMapping("/albums")
@Validated
public class AlbumController {
    @Autowired
    private IAlbumService albumService;

    /**
     * 根据相册id获取全部图片
     */
    @GetMapping("/{id}/pictures")
    @ApiOperation("根据相册id获取全部图片")
    @ApiImplicitParam(name = "id", value = "相册id", readOnly = true, dataType = "long")
    public JsonResult<List<PictureListItemVO>> getPicturesByAlbumId(@PathVariable Long id) {
        log.debug("开始处理【根据相册id获取全部图片】的请求, 参数: {}", id);
        List<PictureListItemVO> list = albumService.getPictureListByAlbumId(id);
        return JsonResult.ok(list);
    }

    /**
     * 添加相册
     */
    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiResponse(code = 20000, message = "操作成功", response = JsonResult.class)
    public JsonResult<Long> addNew(@Valid @RequestBody AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理【添加相册】的请求, 参数: {}", albumAddNewDTO);
        Long id = albumService.addNew(albumAddNewDTO);
        return JsonResult.ok(id);
    }
    /**
     * 为相册添加图片
     */
    @PostMapping("/pictures/add-new")
    @ApiOperation("为相册添加图片")
    public JsonResult<Void> addPicture(@RequestBody PictureAddNewDTO pictureAddNewDTO) {
        log.debug("开始处理【为相册添加图片】的请求, 参数: {}", pictureAddNewDTO);
        albumService.addPicture(pictureAddNewDTO);
        return JsonResult.ok();
    }
    @PostMapping("/{albumId}/pictures/{pictureId}/set-cover")
    @ApiOperation("设置相册封面")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "albumId", value = "相册ID", readOnly = true, dataType = "long"),
            @ApiImplicitParam(name = "pictureId", value = "图片ID", readOnly = true, dataType = "long"),
    })
    public JsonResult<Void> setCover(@PathVariable Long albumId, @PathVariable Long pictureId) {
        log.debug("开始处理【设置相册封面】的请求, 参数: {}, {}", albumId, pictureId);
        albumService.setCover(albumId, pictureId);
        return JsonResult.ok();
    }

    /**
     * 删除相册图片
     */
    @PostMapping("/{albumId}/pictures/{pictureId}/delete")
    @ApiOperation("删除相册图片")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "albumId", value = "相册ID", readOnly = true, dataType = "long"),
            @ApiImplicitParam(name = "pictureId", value = "图片ID", readOnly = true, dataType = "long"),
    })
    public JsonResult<Void> deletePicture(@PathVariable Long albumId, @PathVariable Long pictureId) {
        log.debug("开始处理【删除相册图片】的请求, 参数: {}, {}", albumId, pictureId);
        albumService.deletePicture(albumId, pictureId);
        return JsonResult.ok();
    }
}
